import scrapy
from bilbil.items import BilbilItem
from bs4 import BeautifulSoup
import pandas as pd
from openpyxl import Workbook


class BilidanmuspiderSpider(scrapy.Spider):
    name = 'BilidanmuSpider'
    allowed_domains = ['www.bilibili.com']
    start_url = 'https://comment.bilibili.com/355081548.xml'

    def start_requests(self):
        yield scrapy.Request(self.start_url, callback=self.parse_cid)

    def parse_cid(self, response):
        content = response.text
        soup = BeautifulSoup(content, 'lxml')
        p = soup.find_all('d')
        danmu = [i.text for i in p]
        data = [i.get('p').split(',') for i in p]

        for i, u in zip(danmu, data):
            bilbil = BilbilItem()
            bilbil['出现时间'] = u[0]
            bilbil['模式'] = u[1]
            bilbil['字号'] = u[2]
            bilbil['颜色'] = u[3]
            bilbil['评论时间'] = u[4]
            bilbil['弹幕池'] = u[5]
            bilbil['用户ID'] = u[6]
            bilbil['rowID'] = u[7]
            bilbil['弹幕内容'] = i
            yield bilbil



    def parse(self, response):
        pass
